package com.itita.weibo.ui;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itita.weibo.manager.IWeiboActivity;
import com.itita.weibo.manager.MainService;
import com.itita.weibo.model.ImageFaceAdapter;
import com.itita.weibo.model.Task;
import com.itita.weibo.util.WeiboUtil;

public class SendSuggestionToAuthorActivity extends Activity implements  IWeiboActivity{
	
	// 发送微博的结果
    public static final int REF_NEW_WEIBO_RESULT = 1;

	private EditText etNewBlog;
    
    private GridView gridView;
    
    public boolean hasPic=false;
    
    public boolean hasGPS=false;
    
    public boolean isFace = false;
    
    public byte dat[];//图片数据
    
    public double gps[];//经纬度数据 
	 
    private RelativeLayout facesLayout;
    
	//得到顶部的按钮设定返回和发表
	private View titleview;
	
	private TextView tvTextLimit;
	
	//添加图片按钮 
	private ImageButton btaddPic;
	//返回
	private Button btnBack ;
	
	 //发表  
	private Button btnSend;
    
    //添加经纬度
	private ImageButton btaddGPS;
    
	//添加表情
	private ImageButton btnFaces ;

    //插入话题
	private ImageButton ibInsertTopic;
    
    //插入用户名
	private ImageButton ibInsertAt;
    
	//发布微博进度条
//	private View progress4NewProgress;
	
	private Toast sendToast;
	
	protected void onCreate(Bundle savedInstanceState) {
		 
		super.onCreate(savedInstanceState);
		//取消标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		this.setContentView(R.layout.newsuggestionblog);

		this.initControlsAndAddControlsEvent();
		
		//
		MainService.mainService.addActivity(this);
	}
	
	/**
	 * 初始化控件和为控件添加事件
	 */
	public void initControlsAndAddControlsEvent() {
		//得到输入文字的文本框
		this.etNewBlog = (EditText)this.findViewById(R.id.etNewBlog);
		this.titleview = this.findViewById(R.id.title4NewSuggestionBlog);
		this.tvTextLimit  = (TextView) this.findViewById(R.id.tvTextLimit);
		
		this.btnBack = (Button)titleview.findViewById(R.id.title_bt_left);
		 //发表  
		this.btnSend=(Button)titleview.findViewById(R.id.title_bt_right);
//		this.btnSend.setText("发表");
		this.btnSend.setBackgroundResource(R.drawable.title_post);
		((TextView)titleview.findViewById(R.id.textView)).setText("意见反馈");
		
//		 final ImageView imageViewTitle = (ImageView) titleview.findViewById(R.id.imageViewTitle);
//		 imageViewTitle.setImageResource(R.drawable.feedback);
		 
		this.btnBack.setBackgroundResource(R.drawable.title_back);
//		this.btnBack.setText("返回");
		
	    //进度条视图 
//		this.progress4NewProgress = this.findViewById(R.id.progress4NewProgress);
//		this.progress4NewProgress.setVisibility(View.GONE);
		//添加发表私信事件
	    this.btnSend.setOnClickListener(new OnClickListener(){     
			
	    	public void onClick(View arg0) {
	    		String blogContent = etNewBlog.getText().toString();
	    		  if(!WeiboUtil.strIsNotEmpty(blogContent)){
	    			  WeiboUtil.myToastShow(SendSuggestionToAuthorActivity.this, "您要对我们说点什么吗?");
	    		      return;
	    		  }
	    		  //显示发送微博进度条
				sendToast = WeiboUtil.myToastShowProceessBar(
						SendSuggestionToAuthorActivity.this, "反馈信息发送中...");
				sendToast.show();
				HashMap hm = new HashMap();
				hm.put("msg", blogContent);
				Task ts = new Task(Task.TASK_NEW_SUGGESTION_TO_AUTHOR_WEIBO, hm);
				MainService.newTaskToTaskQueneHead(ts);  
			}
	    });
		
	
		 
        
		//添加返回事件
		this.btnBack.setOnClickListener(new OnClickListener(){
					public void onClick(View v) {
		 	    	   finish();
			       }
			    });
		this.etNewBlog.addTextChangedListener(new TextWatcher(){

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if(s.length() >= 140){
					Toast.makeText(SendSuggestionToAuthorActivity.this, "字数上线140", Toast.LENGTH_SHORT).show();
					return;
				}
				int i=140-s.length();
//				tvTextLimit.setText("可输入字数:"+i);
				tvTextLimit.setText(String.valueOf(i));
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
			
		}); 
	 
	   
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void refresh(Object... param) {
		sendToast.cancel();
		sendToast = null;
		if((Integer)param[0] == REF_NEW_WEIBO_RESULT){
			//关闭进度条
//			this.progress4NewProgress.setVisibility(View.GONE);
			//发送结果
//			String msg = (String) param[1];
//			Intent intent = new Intent();
//			/* 取得Bundle对象中的数据 */
//			Bundle bunde = new Bundle();
//			bunde.putString("newBlogResult", msg);
//			intent.putExtras(bunde);
//			this.setResult(RESULT_OK, intent);
			WeiboUtil.myToastShow(this, (String) param[1]);
			this.finish();
		}
	}

	

   
}
